from flask_avatars import Identicon

from exts import db
from datetime import datetime
from enum import Enum
from shortuuid import uuid
from werkzeug.security import generate_password_hash,check_password_hash

class PermissionEnum(Enum):
    """
    权限枚举
    """
    BOARD = '板块'
    POST = '帖子'
    COMMENT = '评论'
    FRONT_USER = '前台用户'
    CMS_USER = '后台用户'


class PermissionModel(db.Model):
    __tablename__ = 'permission'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='权限id')
    name = db.Column(db.Enum(PermissionEnum), nullable=False, unique=True, comment='权限名称')


role_permission_table = db.Table('role_permission_table',
                                 db.Column('role_id', db.Integer, db.ForeignKey('role.id'), comment='角色id'),
                                 db.Column('permission_id', db.Integer, db.ForeignKey('permission.id'),
                                           comment='权限id'))


class RoleModel(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='角色id')
    name = db.Column(db.String(50), nullable=False, comment='角色名称')
    desc = db.Column(db.String(200), nullable=True, comment='角色描述')
    creat_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    permissions = db.relationship('PermissionModel', secondary=role_permission_table, backref='roles')


class UserModel(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.String(100), primary_key=True, default=uuid, comment='用户id')
    username = db.Column(db.String(50), nullable=False, unique=True, comment='用户名')
    _password = db.Column(db.String(200), nullable=False, comment='密码')
    email = db.Column(db.String(50), nullable=False, unique=True, comment='邮箱')
    avatar = db.Column(db.String(100), comment='用户头像')
    signature = db.Column(db.String(100), comment='个性签名')
    join_time = db.Column(db.DateTime, default=datetime.now, comment='加入时间')
    is_staff = db.Column(db.Boolean, default=False, comment='是否是员工')
    is_active = db.Column(db.Boolean, default=True, comment='是否可用')
    is_admin = db.Column(db.Boolean, default=False, comment='是否是管理员')
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'), comment='角色id')
    role = db.relationship('RoleModel', backref='users')
    avatar_s = db.Column(db.String(64), comment='用户头像s')
    avatar_m = db.Column(db.String(64), comment='用户头像m')
    avatar_l = db.Column(db.String(64), comment='用户头像l')

    def __init__(self,*args,**kwargs):
        if 'password' in kwargs:
            self.password = kwargs.get('password')
            kwargs.pop('password')
        super().__init__(*args, **kwargs)
        self.generate_avatar()


    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, raw_password):
        self._password = generate_password_hash(raw_password)

    def check_password(self, raw_password):
       result =  check_password_hash(self.password, raw_password)
       return result

    def has_permission(self, permission):
        """
        判断用户是否具有某个权限
        :param permission:
        :return:
        """
        return permission in [permission.name for permission in self.role.permissions]

    def generate_avatar(self):
        avatar = Identicon()
        filenames = avatar.generate(text=self.email)
        self.avatar_s = filenames[0]
        self.avatar_m = filenames[1]
        self.avatar_l = filenames[2]
        db.session.commit()


